﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

/// <summary>
/// 常规指标维护
/// Update By:Allen Jiang
/// Update Time:2008-12-26
/// Description:
///     修改常规指标维护在一个页面。
/// </summary>
public partial class InStoreDataMeasures_MeasuresDetail_List : PageBase
{
    private const string MeasuresDetail_List = "MeasuresDetail_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMeasuresDetailBind();
        }
    }

   

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetMeasuresDetailBind()
    {
        string where = "";

        if (ddl_active.SelectedValue != "")
        {
            where += " AND ACTIVE="+ddl_active.SelectedValue;
        }

        if (txt_key.Text != "")
        {
            where += " AND ( NAME LIKE '%"+txt_key.Text+"%' OR CODE LIKE '%"+txt_key.Text+"%') ";
        }


       GridPager1.DataSource = new MeasuresDetailManager().GetMeasuresDetailTable(MyStateManager.DomainID,where);
       GridPager1.GridView = GridView1;
       GridPager1.BindData();
       GridPager1.Visible = GridView1.Rows.Count > 0;
       
    }
  
    /// <summary>
    /// 保存数据事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
       
        try
        {
            MEASURE_DETAIL Measuredetail = null;
            MeasuresDetailManager manager = new MeasuresDetailManager();

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                Measuredetail = new MEASURE_DETAIL();
                Measuredetail.CREATETIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            }
            else
            {
                Measuredetail = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox txtName = DetailsView1.FindControl("txtName") as TextBox;
            TextBox txt_DROPDOWNVALUE = DetailsView1.FindControl("txt_DROPDOWNVALUE") as TextBox;
            DropDownList DDL_INPUTTYPE = DetailsView1.FindControl("DDL_INPUTTYPE") as DropDownList;
            DropDownList ddlPhotoType = DetailsView1.FindControl("ddlPhotoType") as DropDownList;
            CheckBox CHK_ISPHOTO = DetailsView1.FindControl("CHK_ISPHOTO") as CheckBox;
            CheckBox CHK_ISREQUIRED = DetailsView1.FindControl("CHK_ISREQUIRED") as CheckBox;
            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            Measuredetail.CODE = txtCode.Text;
            Measuredetail.NAME = txtName.Text;
            Measuredetail.INPUTTYPE = decimal.Parse(DDL_INPUTTYPE.SelectedValue);//显示类型
            if (Measuredetail.INPUTTYPE == 5 || Measuredetail.INPUTTYPE == 1)
                Measuredetail.DROPDOWNVALUE = "|"+txt_DROPDOWNVALUE.Text.Replace("\n","|");//下拉框值
            
            Measuredetail.ISPHOTO = CHK_ISPHOTO.Checked;//是否强制拍照
            if(Measuredetail.ISPHOTO)
                Measuredetail.PHOTOTYPEID = ddlPhotoType.SelectedValue == "" ? 0 : decimal.Parse(ddlPhotoType.SelectedValue);
            
            Measuredetail.ISREQUIRED = CHK_ISREQUIRED.Checked;//是否必填
            Measuredetail.ACTIVE = cbxValid.Checked;
            Measuredetail.DOMAINID = MyStateManager.DomainID;
            Measuredetail.LAST_UPDATER = MyStateManager.UserID.ToString();
            Measuredetail.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
          
            string LogContent = "";

            if (Measuredetail.State == DataRowState.Added)
            {
                LogContent += "添加指标明细：" + txtCode.Text;
            }
            else
            {
                LogContent += Measuredetail.CODE + "信息发生变动，变动后信息是：";
                LogContent += "指标明细代码：" + Measuredetail.CODE;
                LogContent += "指标明细名称：" + Measuredetail.NAME;
                LogContent += "有效：" + Measuredetail.ACTIVE;
                LogContent += "操作人：" + Measuredetail.LAST_UPDATER;
            }
            manager.Save(Measuredetail);

            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.AUTOSYNC,
            MyStateManager.Name, MyStateManager.UserID, LogContent);
            ShowMessage("保存成功", Request.Url.ToString());
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "指标明细新增 - 异常:" + ex.Message);
        }
    }


    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
      
    }
    /// <summary>
    /// 绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;
        CheckBox CHK_ISPHOTO = DetailsView1.FindControl("CHK_ISPHOTO") as CheckBox;
        DropDownList DDL_INPUTTYPE = DetailsView1.FindControl("DDL_INPUTTYPE") as DropDownList;
        TextBox txt_DROPDOWNVALUE = DetailsView1.FindControl("txt_DROPDOWNVALUE") as TextBox;
        DropDownList ddlPhotoType = DetailsView1.FindControl("ddlPhotoType") as DropDownList;
        DictionaryItemsManager dim = new DictionaryItemsManager();

        ddlPhotoType.DataTextField = "Name";
        ddlPhotoType.DataValueField = "DICTIONARYITEMID";
        ddlPhotoType.DataSource = dim.GetItemList(DictionaryEnum.PhotoType, this.MyStateManager.DomainID);
        ddlPhotoType.DataBind();
        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
           
        }
        else
        {
            MEASURE_DETAIL md=DetailsView1.DataItem as MEASURE_DETAIL;

            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            txtCode.Enabled = false;

           

            ddlPhotoType.SelectedValue = md.PHOTOTYPEID == null ? "0" : md.PHOTOTYPEID.ToString();

            DDL_INPUTTYPE.SelectedValue = md.INPUTTYPE==null?"0":md.INPUTTYPE.ToString();
            if (!(md.INPUTTYPE == 1 || md.INPUTTYPE == 5))
            {
                txt_DROPDOWNVALUE.Attributes.Add("disabled", "true");
            }
        }
        DDL_INPUTTYPE.Attributes.Add("onchange", "selectPhoto(this,'" + CHK_ISPHOTO.ClientID + "','" + txt_DROPDOWNVALUE.ClientID + "','" + ddlPhotoType.ClientID + "')");
    }
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
   
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetMeasuresDetailBind();
    }
   
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetMeasuresDetailBind();
    }

    public string getInputType(string v)
    {
       return new MeasuresGroupItemDetailManager().getInputType(v);
    }

    public string getPhotoType(string v)
    {


        DataTable dt = new DictionaryItemsManager().GetDictionaryItemByDictCodeAndItemCode(DictionaryEnum.PhotoType, v, MyStateManager.DomainID).Tables[0];
        if(dt!=null&&dt.Rows.Count>0)
        {
            return dt.Rows[0]["NAME"].ToString();
        }
        return "";
    }

    public string GetDropdownValue(string v)
    {
        if (!string.IsNullOrEmpty(v))
        {
            string split = v.Substring(0, 1);
            v = v.Substring(1);
            v = v.Replace(split, "\n");
        }

        return v;
    }

}
